﻿using System;
using System.Security.Cryptography;
using System.Text;
using WebApplication11.Dao;
using WebApplication11.Models;

namespace WebApplication11.Services
{
    public class userService
    {
        userDao dao = new userDao();
        public int Login(user usermsg)
        {
            return dao.Login(usermsg.Username, md5Encrypt(usermsg.Password));
        }
        
        static string md5Encrypt(string str)
        {
            MD5 md5 = MD5.Create();
            byte[] buffer = Encoding.UTF8.GetBytes(str); //将字符串转成字节数组
            byte[] byteArray = md5.ComputeHash(buffer); //调用加密方法
            StringBuilder sb = new StringBuilder();
            foreach (byte b in byteArray) //遍历字节数组
            {
                sb.Append(b.ToString("x2")); //将字节数组转成16进制的字符串。X表示16进制，2表示每个16字符占2位
            }
            return sb.ToString();
        }

        public bool Register(user form)
        {
            try
            {
                dao.Register(form);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}